Imports NUnit.Framework
Imports pixolut.BizBlox
Imports System.Web.UI.WebControls


<TestFixture()> Public Class DOCollectionTests
    Public Sub New()
        MyBase.New()
    End Sub

    <TestFixtureSetUp()> Public Sub GlobalInit()
    End Sub

    <TestFixtureTearDown()> Public Sub GlobalDestruct()
    End Sub

    Dim sObj As Student
    Dim sCol As StudentCollection
    <SetUp()> Public Sub LocalInit()
        sObj = Setup.CreateTestStudent()
        sCol = New StudentCollection
    End Sub

    <TearDown()> Public Sub LocalDestruct()
        Setup.ClearDataSet()
    End Sub

    <Test()> Public Sub testFind()
        sCol.FindByStudentName(Setup.TestObjectName)
        Assert.AreEqual(sCol.Count, 1, "Verify return count")
        Assert.AreEqual(sCol(0).StudentID.ToString(), sObj.StudentID.ToString(), "Verify PK at index 0")
    End Sub

    <Test(), ExpectedException(GetType(DOException))> Public Sub testFindNoInit()
        Dim xObj As Student = sCol(0)
    End Sub

    <Test(), ExpectedException(GetType(IndexOutOfRangeException))> Public Sub testFindFail()
        sCol.FindByStudentName("not here")
        Dim xObj As Student = sCol(0)
    End Sub


    <Test(), ExpectedException(GetType(IndexOutOfRangeException))> Public Sub testItemFailOverbound()
        sCol.FindByStudentName(Setup.TestObjectName)
        Dim xObj As Student = sCol(9)
    End Sub

    <Test(), ExpectedException(GetType(IndexOutOfRangeException))> Public Sub testItemFailUnderbound()
        sCol.FindByStudentName(Setup.TestObjectName)
        Dim xObj As Student = sCol(-1)
    End Sub

    <Test()> Public Sub testIndexOfGuid()
        sCol.FindByStudentName(Setup.TestObjectName)
        Dim i As Integer = sCol.IndexOf(sObj.StudentID)
        Assert.AreEqual(i, 0, "Verify index")
    End Sub

    <Test()> Public Sub testIndexOfGuidFail()
        sCol.FindByStudentName(Setup.TestObjectName)
        Dim i As Integer = sCol.IndexOf(New Guid)
        Assert.AreEqual(i, -1, "Verify index")
    End Sub

    <Test()> Public Sub testIndexOfAVPair()
        sCol.FindByStudentName(Setup.TestObjectName)
        Dim i As Integer = sCol.IndexOf(Student.Columns.Name, Setup.TestObjectName)
        Assert.AreEqual(i, 0, "Verify index by property")
    End Sub

    <Test(), ExpectedException(GetType(NullReferenceException))> Public Sub testIndexOfAVPairFailNull()
        sCol.FindByStudentName(Setup.TestObjectName)
        Dim i As Integer = sCol.IndexOf(Nothing, Nothing)
        Assert.AreEqual(i, 0, "Verify index by property")
    End Sub


    <Test()> Public Sub testIndexOfAVPairFail()
        sCol.FindByStudentName(Setup.TestObjectName)
        Dim i As Integer = sCol.IndexOf(Student.Columns.Name, "not there")
        Assert.AreEqual(i, -1, "Verify index by property")
    End Sub

    <Test()> Public Sub testPKExists()
        sCol.FindByStudentName(Setup.TestObjectName)
        Assert.AreEqual(sCol.PKExists(sObj.StudentID), True, "Verify PK Exists")
    End Sub

    <Test()> Public Sub testPKExistsFail()
        sCol.FindByStudentName(Setup.TestObjectName)
        Assert.AreEqual(sCol.PKExists(New Guid), False, "Verify PK Exists")
    End Sub

    <Test()> Public Sub testCreateDataView()
        sCol.FindByStudentName(Setup.TestObjectName)
        Dim dv As DataView = sCol.CreateDataView(True, Student.Columns.Name)
        Assert.AreEqual(CStr(dv(0)("Name")), Setup.TestObjectName, "Verify Name is addressable and correct")
        Assert.AreEqual(CType(dv(0)("StudentID"), Guid).ToString(), sObj.StudentID.ToString(), "Verify PK is correct")
    End Sub

    <Test()> Public Sub testBindDataSource()
        sCol.FindByStudentName(Setup.TestObjectName)
        Dim ddl As New Web.UI.WebControls.DropDownList
        sCol.BindDataSource(Student.Columns.Name, ddl)
        Assert.AreEqual(ddl.Items(0).Text, Setup.TestObjectName)
        Assert.AreEqual(ddl.Items(0).Value, sObj.StudentID.ToString())
    End Sub

    <Test()> Public Sub testBindEmptyDataSource()
        sCol.FindByStudentName("not there")
        Dim ddl As New Web.UI.WebControls.DropDownList
        sCol.BindDataSource(Student.Columns.Name, ddl)
        Assert.AreEqual(ddl.Items.Count, 0)
    End Sub

    'first line in the combo box is -1 ("hello")
    <Test()> Public Sub testBindEmptyDataSourceWithInfoRow()
        sCol.FindByStudentName("not there")
        Dim ddl As New Web.UI.WebControls.DropDownList
        sCol.BindDataSource(Student.Columns.Name, ddl, "hello")
        Assert.AreEqual(ddl.Items(0).Text, "hello")
        Assert.AreEqual(ddl.Items.Count, 1)
    End Sub


    'first line in the combo box is -1 ("hello")
    <Test()> Public Sub testBindDataSourceWithInfoRow()
        sCol.FindByStudentName(Setup.TestObjectName)
        Dim ddl As New Web.UI.WebControls.DropDownList
        sCol.BindDataSource(Student.Columns.Name, ddl, "hello")
        Assert.AreEqual(ddl.Items(0).Text, "hello")
        Assert.AreEqual(ddl.Items(1).Text, Setup.TestObjectName)
        Assert.AreEqual(ddl.Items(1).Value, sObj.StudentID.ToString())
    End Sub


    <Test()> Public Sub testResetCurrentQuery()
        Dim freshBuilder As SQLBuilder = sCol.GetSqlBuilder
        Dim mySQL As SQLBuilder = sCol.GetSqlBuilder()
        mySQL.addWhere(Student.Columns.Age, "10")
        sCol.SetCurrentQuery(mySQL)
        sCol.ResetCurrentQuery()
        Dim resetBuilder As SQLBuilder = sCol.GetCurrentQuery()
        Assert.AreEqual(freshBuilder.RenderSelect, resetBuilder.RenderSelect)
    End Sub

End Class
